/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Technicalcollection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Monitoring;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class TechnicalcollectionJpaController {

    public TechnicalcollectionJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Technicalcollection technicalcollection) {
        if (technicalcollection.getMonitoringList() == null) {
            technicalcollection.setMonitoringList(new ArrayList<Monitoring>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Monitoring> attachedMonitoringList = new ArrayList<Monitoring>();
            for (Monitoring monitoringListMonitoringToAttach : technicalcollection.getMonitoringList()) {
                monitoringListMonitoringToAttach = em.getReference(monitoringListMonitoringToAttach.getClass(), monitoringListMonitoringToAttach.getIdmonitoring());
                attachedMonitoringList.add(monitoringListMonitoringToAttach);
            }
            technicalcollection.setMonitoringList(attachedMonitoringList);
            em.persist(technicalcollection);
            for (Monitoring monitoringListMonitoring : technicalcollection.getMonitoringList()) {
                Technicalcollection oldTechnicalcollectionOfMonitoringListMonitoring = monitoringListMonitoring.getTechnicalcollection();
                monitoringListMonitoring.setTechnicalcollection(technicalcollection);
                monitoringListMonitoring = em.merge(monitoringListMonitoring);
                if (oldTechnicalcollectionOfMonitoringListMonitoring != null) {
                    oldTechnicalcollectionOfMonitoringListMonitoring.getMonitoringList().remove(monitoringListMonitoring);
                    oldTechnicalcollectionOfMonitoringListMonitoring = em.merge(oldTechnicalcollectionOfMonitoringListMonitoring);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Technicalcollection technicalcollection) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Technicalcollection persistentTechnicalcollection = em.find(Technicalcollection.class, technicalcollection.getIdtechnicalcollection());
            List<Monitoring> monitoringListOld = persistentTechnicalcollection.getMonitoringList();
            List<Monitoring> monitoringListNew = technicalcollection.getMonitoringList();
            List<Monitoring> attachedMonitoringListNew = new ArrayList<Monitoring>();
            for (Monitoring monitoringListNewMonitoringToAttach : monitoringListNew) {
                monitoringListNewMonitoringToAttach = em.getReference(monitoringListNewMonitoringToAttach.getClass(), monitoringListNewMonitoringToAttach.getIdmonitoring());
                attachedMonitoringListNew.add(monitoringListNewMonitoringToAttach);
            }
            monitoringListNew = attachedMonitoringListNew;
            technicalcollection.setMonitoringList(monitoringListNew);
            technicalcollection = em.merge(technicalcollection);
            for (Monitoring monitoringListOldMonitoring : monitoringListOld) {
                if (!monitoringListNew.contains(monitoringListOldMonitoring)) {
                    monitoringListOldMonitoring.setTechnicalcollection(null);
                    monitoringListOldMonitoring = em.merge(monitoringListOldMonitoring);
                }
            }
            for (Monitoring monitoringListNewMonitoring : monitoringListNew) {
                if (!monitoringListOld.contains(monitoringListNewMonitoring)) {
                    Technicalcollection oldTechnicalcollectionOfMonitoringListNewMonitoring = monitoringListNewMonitoring.getTechnicalcollection();
                    monitoringListNewMonitoring.setTechnicalcollection(technicalcollection);
                    monitoringListNewMonitoring = em.merge(monitoringListNewMonitoring);
                    if (oldTechnicalcollectionOfMonitoringListNewMonitoring != null && !oldTechnicalcollectionOfMonitoringListNewMonitoring.equals(technicalcollection)) {
                        oldTechnicalcollectionOfMonitoringListNewMonitoring.getMonitoringList().remove(monitoringListNewMonitoring);
                        oldTechnicalcollectionOfMonitoringListNewMonitoring = em.merge(oldTechnicalcollectionOfMonitoringListNewMonitoring);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = technicalcollection.getIdtechnicalcollection();
                if (findTechnicalcollection(id) == null) {
                    throw new NonexistentEntityException("The technicalcollection with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Technicalcollection technicalcollection;
            try {
                technicalcollection = em.getReference(Technicalcollection.class, id);
                technicalcollection.getIdtechnicalcollection();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The technicalcollection with id " + id + " no longer exists.", enfe);
            }
            List<Monitoring> monitoringList = technicalcollection.getMonitoringList();
            for (Monitoring monitoringListMonitoring : monitoringList) {
                monitoringListMonitoring.setTechnicalcollection(null);
                monitoringListMonitoring = em.merge(monitoringListMonitoring);
            }
            em.remove(technicalcollection);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Technicalcollection> findTechnicalcollectionEntities() {
        return findTechnicalcollectionEntities(true, -1, -1);
    }

    public List<Technicalcollection> findTechnicalcollectionEntities(int maxResults, int firstResult) {
        return findTechnicalcollectionEntities(false, maxResults, firstResult);
    }

    private List<Technicalcollection> findTechnicalcollectionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Technicalcollection.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Technicalcollection findTechnicalcollection(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Technicalcollection.class, id);
        } finally {
            em.close();
        }
    }

    public int getTechnicalcollectionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Technicalcollection> rt = cq.from(Technicalcollection.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
